package clientServlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import validate.Validate;

import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.PhoneNumber;

import controller.Controller;

@SuppressWarnings("serial")
public class ChangePropertiesClient extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("sign in get =)");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("email "+req.getParameter("email"));
		String userName = (String) req.getSession().getAttribute("userNameClient");
		String password = req.getParameter("password");
		String password1 = req.getParameter("password1");
		Email email = new Email(req.getParameter("email"));
		PhoneNumber phone = new PhoneNumber(req.getParameter("phone"));
		
		System.out.println("client properties");
		
		String validString = Validate.checkClientValid(userName, phone, email, password, password1);
		
		if (validString.equals(""))
		{
			int result = Controller.getInstance().updateClientAccount(userName, password, phone, email);
			
			switch (result)
			{
			case 0:
				req.getSession().removeAttribute("validErrorClient");
				resp.sendRedirect("/allClientOrders.jsp");
				break;
			case 1:
				req.getSession().setAttribute("validErrorClient","username not exists");
				resp.sendRedirect("/home.jsp#join");
				break;
			}
				
		}
		else
		{
			req.getSession().setAttribute("validErrorClient",validString);
			resp.sendRedirect("/home.jsp#join");
		}
		
		

	}
	
}
